<?php
/**
 * [Fengxu!] (C)2008-2009 SSBT Inc.
 * This is NOT a freeware, use is subject to license terms
 * $Id: natdispos.php Mon Apr 07 06:46:03 GMT 2008 $
 * @author fengxu qq:250484464 <zhiming0824@163.com>
 * @version $Id 06:46:03$
 * @license fengxu
 */
require_once 'common.inc.php';
!in_array('5',$larr)&&adminmsg('你没有被赋予操作该项的权限！');
$F_P = chr(7).chr(7);
if($action=='addrule')
{
	if(!$addrulesubmit)
	{
		include MCC_ROOT.'/templates/msystem/natadd.htm';exit;
	}
	if(empty($regscope)&&empty($regscope)) adminmsg('规则和IP范围必填一个','natdispos.php?action=addrule');
	$ipscope = trim($ipscope);
	$infip = $ipscope;
	$ipscope = str_replace("\r\n",$F_P,$ipscope);
	if(!empty($regscope))
	{
		$regscope = trim($regscope);
		$infreg = $regscope;
		$regscope = str_replace("\r\n",$F_P,$regscope);		
	}
	$toip = trim($toip);
	$toport = intval($toport);
	$query=$db->query("SELECT count(rid) FROM msys_rule WHERE toip='$toip' AND toport='$toport'");
	$iprt = $db->result($query,0);
	if($iprt) adminmsg('你所添加的IP地址已经存在，请在范围列表点击修改','natdispos.php?action=addrule');
	$sql = "INSERT INTO msys_rule (toip,toport,ipscope,iprule,lastmodify,operation,uid,ruletype) VALUES ('$toip','$toport','$infip','$infreg','$timestamp','','$msys_uid','0')";
	$db->query($sql);
	$ipinfo = total_order(6,4,0,0,33,$ipscope,$toip);
	order_send($ipinfo);
	$reginfo = total_order(6,4,0,0,34,$regscope,$toip);
	order_send($reginfo);	
	adminmsg('添加成功请点击返回','natdispos.php?action=natlist');
}elseif ($action=='modifyrule')
{
	if(!$modifyrulesubmit)
	{
		$rid = intval($rid);
		$query = $db->query("SELECT * FROM msys_rule WHERE rid='$rid'");
		if(!$query) aminmsg('你所要修改的规则不存在，或则已经删除','natdispos.php?action=natlist');	
		$riddb = array();
		$riddb = $db->fetch_array($query);
		include MCC_ROOT.'/templates/msystem/natadd.htm';
	}else {
		if(!eregi("^([0-9]{1,3}\.){3}[0-9]{1,3}$",$toip))
		{
			adminmsg('IP地址格式错误，就修改',"natdispos.php?action=modifyrule&rid=$rid");
		}
		$rid = intval($rid);
		$ipscope = trim($ipscope);
		$regscope = trim($regscope);
		$query = $db->query("SELECT * FROM msys_rule WHERE rid='$rid'");
		if(!$query) aminmsg('你所要修改的规则不存在，或则已经删除','natdispos.php?action=natlist');	
		$riddb = array();
		$riddb = $db->fetch_array($query);
		if($ipscope!=$riddb['ipscope'])
		{
			$ipscope_s = str_replace("\r\n",$F_P,$ipscope);
			$ipinfo = total_order(6,4,0,0,33,$ipscope_s,$toip);
			order_send($ipinfo);
		}
		if($regscope!=$riddb['regscope'])
		{
			$regscope_s = str_replace("\r\n",$F_P,$regscope);
			$reginfo = total_order(6,4,0,0,34,$regscope_s,$toip);
			order_send($reginfo);
		}
		$db->query("UPDATE msys_rule SET ipscope='$ipscope',iprule='$regscope',toip='$toip' WHERE rid = '$rid'");
		adminmsg('修改成功，请点击返回',"natdispos.php?action=modifyrule&rid=$rid");
	}
}elseif ($action=='delrule')
{
	$delrid = intval($delrid);
	$query = $db->query("SELECT * FROM msys_rule WHERE rid='$rid'");
	if(!$query) aminmsg('已经被删除','natdispos.php?action=natlist');
	else 
	$db->query("DELETE FROM msys_rule WHERE rid = '$rid'");
	adminmsg('删除成功，请点击返回','natdispos.php?action=natlist');
}elseif ($action=='natlist')
{
	(!$page || $page<0 || !is_numeric($page))&&$page = 1;
	$page = intval($page);
	$start_limit = ($page-1)*2;
	$numq = $db->query("SELECT * FROM msys_rule WHERE ruletype='0'");
	$nums = $db->num_rows($numq);
	$ppp = ceil($nums/2);
	$sql = "SELECT * FROM msys_rule WHERE ruletype='0' LIMIT $start_limit,2";
	$query = $db->query($sql);
	$ruledb = array();
	while ($rl = $db->fetch_array($query))
	{
		$ruledb[] = $rl;
	}
	$pages = numofpage($nums,$page,$ppp,'natdispos.php?action=natlist&');
	include MCC_ROOT.'/templates/msystem/natlist.htm';
}
?>